import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// 引入unocss
import UnoCSS from 'unocss/vite'
// 引入mock
import { viteMockServe } from 'vite-plugin-mock'
// 样式/组件自动引入
import AutoImport from 'unplugin-auto-import/vite'
// 自定义图标文件路径
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue(),
      viteMockServe({
        mockPath: './mock', // 设置模拟数据的存储文件夹
        // @ts-ignore
        supportTs: true,//是否读取ts文件模块
        logger: true,//是否在控制台显示请求日志
        localEnabled: true,//设置是否启用本地mock文件
        prodEnabled: false,//设置打包是否启用mock功能 
      }),
      UnoCSS({
        // configFile: './uno.config.ts',
      }),
      AutoImport({
        dts: "src/types/auto-import.d.ts",
        imports: ['vue', 'vue-router', 'pinia'],
      }),
      createSvgIconsPlugin({
        // 自定义图标文件路径
        iconDirs: [
          path.resolve(process.cwd(), 'src/assets/icons/feather'),
          path.resolve(process.cwd(), 'src/assets/icons/local'),
          path.resolve(process.cwd(), 'src/assets/icons/error'),
        ],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'), // process.cwd()当前项目绝对路径
        '~': path.resolve(__dirname),
      }
    },
    // sass变更,此处防止报错
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler'
        }
      },
    },
    server: {
      // 允许IP访问
      host: "0.0.0.0",
      // 应用端口 (默认:5173)
      port: parseInt(env.VITE_APP_PORT),
      // 运行是否自动打开浏览器
      open: true,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          changeOrigin: true,
          target: env.VITE_APP_SERVICE_URL,
          rewrite: (path) => path.replace(new RegExp(`^${env.VITE_APP_BASE_API}`), ''),
        },
      },
    },
  }
})
